package com.example.jegotrip.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toolbar;

import androidx.annotation.Nullable;

import com.example.jegotrip.R;
import com.example.jegotrip.base.BaseActivity;
import com.example.jegotrip.db.NoteDBHelper;
import com.example.jegotrip.entity.NoteInfo;
import com.example.jegotrip.entity.UserInfo;
import com.example.jegotrip.utils.CustomToast;
import com.example.jegotrip.utils.DateUtil;

import java.util.Date;

public class CreateNoteActivity extends BaseActivity {

    private EditText note_title;
    private EditText note_content;
    private Button btn_note_submit;
    private boolean is_login;
    private String login_username;
    private Toolbar toolbar;
    private NoteInfo noteInfo;

    @Override
    protected void initData() {
//        跳转传值
        noteInfo = (NoteInfo) getIntent().getSerializableExtra("noteInfo");
        if (noteInfo != null){
            toolbar.setTitle("编辑备忘录");
            btn_note_submit.setText("编辑");

            note_title.setText(noteInfo.getNote_title());
            note_content.setText(noteInfo.getNote_content());
        }
    }

    @Override
    protected void initViews() {
        note_title = findViewById(R.id.note_title);
        note_content = findViewById(R.id.note_content);
        btn_note_submit = findViewById(R.id.btn_note_submit);
        toolbar = findViewById(R.id.toolBar);
        btn_note_submit = findViewById(R.id.btn_note_submit);
    }

    @Override
    protected int getLayoutResOId() {
        return R.layout.activity_create_note;
    }

    @Override
    protected void setListener() {
        // 点击导航栏返回
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        btn_note_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = note_title.getText().toString();
                String content = note_content.getText().toString();
                if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)){
                    CustomToast.showCenterToast(CreateNoteActivity.this, "请完善信息");
                } else {
                    UserInfo userInfo = UserInfo.getUserInfo();
                    if (noteInfo == null){
                        int row = NoteDBHelper.getInstance(CreateNoteActivity.this).createNote(
                                userInfo.getUsername(),
                                title,
                                content
                        );
                        if (row > 0){
                            CustomToast.showCenterToast(CreateNoteActivity.this, "创建成功");
                        } else {
                            CustomToast.showCenterToast(CreateNoteActivity.this, "创建失败~");
                        }
                    } else {
                        int row = NoteDBHelper.getInstance(CreateNoteActivity.this).editNote(noteInfo.getNote_id(),title,content);
                        if (row > 0){
                            CustomToast.showCenterToast(CreateNoteActivity.this, "修改成功");
                        } else {
                            CustomToast.showCenterToast(CreateNoteActivity.this, "修改失败~");
                        }

                    }
                    setResult(1000);// 修改成功，给一个返回的标识

                    finish();
                }
            }
        });
    }
}
